<?php

/**
 * @Author:      zwh
 * @DateTime:    2015-12-31 13:51:15
 * @Description: 用于维护 该门店的工位信息的信息
 * 默认 能看到该controller 的用户都有权限操作 该 controller 的中的方法 不在controller 对用户权限进行判断
 */
class ScsMainStationController extends BaseController
{
    /*
     * 工位列表
     */
    public function lists(){
    	$validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
    	do{
    		if($validator->fails()) {
                $response = Lang::get("scs_error.station_args_error_31001");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){                
                break;
            }           
            $ms_arr=DB::table("maintenance_station")->leftJoin("maintenance_pads","maintenance_pads.work_station_num","=","maintenance_station.id")->where("maintenance_station.status",1)->where("maintenance_station.shop_id",$sc_user->shop_id)->select("maintenance_station.id","maintenance_station.name","maintenance_station.type","maintenance_pads.imei")->orderBy("maintenance_station.created_at")->get();
            //$sql = DB::getQueryLog();
            //$query = end($sql);
            $temp_arr=array("count"=>sizeof($ms_arr),"station"=>$ms_arr);
            $response = array('code' => 0, 'msg' => 'ok', 'result' => $temp_arr);
    	}while(false);
    	Log::info("info", array("system" => "scs" ,"event" => "stationlist", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    /**
     * @Author:      zwh
     * @DateTime:    2015-12-31 15:47:24
     * @Description: 用于添加门店工位
     */
    public function create(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'name' => 'required',
                'type' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.station_args_error_31001");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){                
                break;
            }
            $name=Input::get("name");
            $t_ms=MaintenanceStation::where("shop_id",$sc_user->shop_id)->where("name",$name)->where("status",1)->first();
            if($t_ms){
                $response = Lang::get("scs_error.station_name_error_31006");
                break;
            }

            try {
                if(Input::get("imei")){
                    $mp=MaintenancePads::where("imei",Input::get("imei"))->where("status",1)->first();
                    if($mp){
                        $mp=MaintenanceShops::find($mp->shop_id);
                        if($mp->shop_id!=$sc_user->shop_id){//如果不是本店 报错
                            $response =Lang::get("scs_error.station_pad_shop_error_31007");
                            $response['msg']='该设备当前归属'.$mp->name.'门店'.$response['msg'];
                            break;
                        }
                        if($mp->work_station_num>0){//说明 该IMEI设备已经绑定了 工位
                            $response = Lang::get("scs_error.station_pad_hasstation_error_31008");
                            break;
                        }
                    }else{
                        $mp=new MaintenancePads();
                        $mp->imei=Input::get("imei");
                        $mp->name=Input::get("imei");
                        $mp->status=1;
                        $mp->shop_id=$sc_user->shop_id;
                        $mp->desc="";
                    }
                }                

                // $ms = new MaintenanceStation();
                // $ms->name = Input::get('name');
                // $ms->status = 1;
                // $ms->shop_id = $sc_user->shop_id;
                // $ms->type = Input::get("type");
                // $ms->save();
                $date=array(
                    "name"=>Input::get("name"),
                    "status"=>1,
                    "shop_id"=>$sc_user->shop_id,
                    "type"=>Input::get("type"),
                );
                $ms_s=MaintenanceStation::saveByDate($date);
                if(Input::get("imei")){
                    $mp->work_station_num=$ms_s->id;
                    $mp->save();
                }               

                $result = array(
                    'id' => $ms_s->id,
                );
            } catch (Exception $e) {
                Log::error("error", array("system" => "scs" ,"event" => "create", 'input' =>Input::all() ,"exception" => $e));
                $response = Lang::get("scs_error.station_create_error_31003");
                break;
            }
            $response = array('code' => 0, 'msg' => 'ok', 'result' => $result);
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "stationcreate", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:      zwh
     * @DateTime:    2015-12-31 16:04:55
     * @Description: 用于修改 工位属性
     */
    public function update(){
        $validator = Validator::make(Input::all(),
            array(
                'id'    => 'required',
                'token' => 'required',
                'name'  => 'required',
                'type'  => 'required',
                // 'imei'  => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.station_args_error_31001");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){                
                break;
            }
            $name=Input::get("name");
            // $t_ms=MaintenanceStation::where("shop_id",$sc_user->shop_id)->where("name",$name)->where("status",1)->first();
            $t_ms=MaintenanceStation::where("id","!=",Input::get('id'))->where("name",$name)->where("shop_id",$sc_user->shop_id)->where("status",1)->first();
            if($t_ms){
                $response = Lang::get("scs_error.station_name_error_31006");
                break;
            }
            try{
                $ms=MaintenanceStation::find(Input::get('id'));
                if(!$ms||$ms->shop_id!=$sc_user->shop_id){
                    $response = Lang::get("scs_error.station_shop_error_31010");
                    break;
                }         
                if(!MaintenanceStation::isFree($ms->id)){//如果工位不是空闲                        
                    $response = Lang::get("scs_error.station_isbusy_error_31009");
                    break;
                }

                $mp=MaintenancePads::where("work_station_num",$ms->id)->where("status",1)->first();//查找原来的工位的pad
                if(!Input::get("imei")){//说明修改解除绑定
                    if($mp){//如果以前存在 pad
                        if(MaintenancePads::isFree($mp->id)){//如果空闲可以修改
                            $mp->work_station_num=0;
                            $mp->status=0;
                            $mp->save();
                        }else{
                            $response = Lang::get("scs_error.pad_isbusy_error_31013");
                            break;
                        }
                    }                    
                }else{//说明是不是解绑
                    if($mp&&$mp->imei==Input::get("imei")){//如果没有修改工位imei
                    }else{//如果修改了工位
                        $new_mp=MaintenancePads::where("imei",Input::get("imei"))->where("status",1)->first();
                        if($new_mp){
                            $mp=MaintenanceShops::find($new_mp->shop_id);
                            if($new_mp->shop_id!=$sc_user->shop_id){//如果不是本店 报错
                                $response = Lang::get("scs_error.station_pad_shop_error_31007");
                                $response['msg']='该设备当前归属'.$mp->name.'门店'.$response['msg'];
                                break;
                            }
                            if($new_mp->work_station_num>0){//说明 该IMEI设备已经绑定了 工位
                                $response = Lang::get("scs_error.station_pad_hasstation_error_31008");
                                break;
                            }
                        }else{
                            $new_mp=new MaintenancePads();
                            $new_mp->imei=Input::get("imei");
                            $new_mp->name=Input::get("imei");
                            $new_mp->status=1;
                            $new_mp->shop_id=$sc_user->shop_id;
                            $new_mp->desc="";
                        }
                        if($mp){//如果以前存在 pad
                            if(MaintenancePads::isFree($mp->id)){//如果空闲可以修改  把自己工位的pad工位号改为0
                                $mp->work_station_num=0;
                                $mp->status=0;
                                $mp->save();
                            }else{
                                $response = Lang::get("scs_error.pad_isbusy_error_31013");
                                break;
                            }
                        }else{//如果以前没有

                        }

                        $new_mp->work_station_num=$ms->id;
                        $new_mp->save();
                    }                    
                }

                
                $ms->name = Input::get('name');
                $ms->type = Input::get('type');
                $ms->save();
                $result = array(
                    'id' => Input::get('id'),
                );
            } catch (Exception $e) {
                Log::error("error", array("system" => "scs" ,"event" => "update", 'input' =>Input::all() ,"exception" => $e));
                $response = Lang::get("scs_error.station_update_error_31004");
                break;
            }
            $response = array('code' => 0, 'msg' => 'ok', 'result' => $result);
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "stationupdate", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:      zwh
     * @DateTime:    2015-12-31 16:08:42
     * @Description: 用于删除工位 信息
     *
     * @Author:      lyt
     * @DateTime:    2016-01-12 14:54:18
     * @Description: fix 用于删除工位的时候 判断工位所有权 修改绑定pad信息
     */
    public function del(){
        $validator = Validator::make(Input::all(),
            array(
                'id'    => 'required',
                'token' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.station_args_error_31001");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){              
                break;
            }
            try {
                $ms = MaintenanceStation::find(Input::get('id'));
                if(!$ms||$ms->shop_id!=$sc_user->shop_id){
                    $response = Lang::get("scs_error.station_shop_error_31010");
                    break;
                }
                if(MaintenanceStation::isFree($ms->id)){//如果空闲可以修改  把自己工位的pad工位号改为0
                    MaintenancePads::where("work_station_num",$ms->id)->update(array("work_station_num"=>0));//解绑工位的pad
                    $ms->status=0;
                    $ms->save();
                    $result = array(
                        'id' => Input::get('id'),
                    );
                }else{
                    $response = Lang::get("scs_error.station_isbusy_error_31009");
                    break;
                }                

            } catch (Exception $e) {
                Log::error("error", array("system" => "scs" ,"event" => "delete", 'input' =>Input::all() ,"exception" => $e));
                $response = Lang::get("scs_error.station_delete_error_31005");
                break;
            }
            $response = array('code' => 0, 'msg' => 'ok', 'result' => $result);
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "stationdelete", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:      zwh
     * @DateTime:    2015-12-31 16:08:42
     * @Description: 用于删除工位 信息
     *
     * @Author:      lyt
     * @DateTime:    2016-01-12 14:54:18
     * @Description: fix 用于删除工位的时候 判断工位所有权 修改绑定pad信息
     */
    public function delete(){
        $validator = Validator::make(Input::all(),
            array(
                'id'    => 'required',
                'token' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.station_args_error_31001");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            try {
                $ms = MaintenanceStation::find(Input::get('id'));
                if(!$ms||$ms->shop_id!=$sc_user->shop_id){
                    $response = Lang::get("scs_error.station_shop_error_31010");
                    break;
                }
                if(MaintenanceStation::isFree($ms->id)){//如果空闲可以修改  把自己工位的pad工位号改为0
                    MaintenancePads::where("work_station_num",$ms->id)->update(array("status"=>0));//解绑工位的pad
                    $ms->status=0;
                    $ms->save();
                    $result = array(
                        'id' => Input::get('id'),
                    );
                }else{
                    $response = Lang::get("scs_error.station_isbusy_error_31009");
                    break;
                }
    
            } catch (Exception $e) {
                Log::error("error", array("system" => "scs" ,"event" => "delete", 'input' =>Input::all() ,"exception" => $e));
                $response = Lang::get("scs_error.station_delete_error_31005");
                break;
            }
            $response = array('code' => 0, 'msg' => 'ok', 'result' => $result);
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "stationdelete", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:      lyt
     * @DateTime:    2016-01-13 13:28:23
     * @Description: 用于展示 门店pad列表
     */
    public function listPad(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.station_args_error_31001");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){              
                break;
            }
            $ms_arr=DB::table("maintenance_pads")->leftJoin("maintenance_station","maintenance_pads.work_station_num","=","maintenance_station.id")->where("maintenance_pads.status",1)->where("maintenance_pads.shop_id",$sc_user->shop_id)->select("maintenance_pads.id","maintenance_station.name as sname","maintenance_pads.name as padname","maintenance_pads.imei","maintenance_pads.desc")->orderBy("maintenance_station.created_at")->get();
            $temp_arr=array("count"=>sizeof($ms_arr),"pads"=>$ms_arr);
            $response = array('code' => 0, 'msg' => 'ok', 'result' => $temp_arr);
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "stationdelete", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


    /**
     * @Author:      lyt
     * @DateTime:    2016-01-13 14:14:06
     * @Description: 删除pad
     */
    public function deletePad(){
        $validator = Validator::make(Input::all(),
            array(
                'id'    => 'required',
                'token' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.station_args_error_31001");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){              
                break;
            }
            try {
                $mp = MaintenancePads::find(Input::get('id'));
                if(!$mp||$mp->shop_id!=$sc_user->shop_id){
                    $response = Lang::get("scs_error.station_pad_shop_error_31007");
                    break;
                }
                if($mp->work_station_num>0){//说明 该pad 绑定的有工位
                    if(!MaintenanceStation::isFree($mp->work_station_num)){//如果空闲可以修改  把自己工位的pad工位号改为0                    
                        $response = Lang::get("scs_error.station_pad_isbusy_error_31011");
                        break;
                    } 
                }
                $mp->status=0;
                $mp->save();
                $result = array(
                    'id' => Input::get('id'),
                );
            } catch (Exception $e) {
                Log::error("error", array("system" => "scs" ,"event" => "delete", 'input' =>Input::all() ,"exception" => $e));
                $response = Lang::get("scs_error.station_delete_error_31005");
                break;
            }
            $response = array('code' => 0, 'msg' => 'ok', 'result' => $result);
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "stationdelete", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
}